﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplicationClasses.Models
{
    public class CadastroModels
    {
        DataClassesDataContext _datacontext = new DataClassesDataContext();

        #region Aluno

        public tbAluno BuscarAluno(int Ra)
        {
            return (from m in _datacontext.tbAlunos
                    where m.Ra == Ra
                    select m).SingleOrDefault();
        }


        public List<tbAluno> ListarAlunos()
        {
            return (from m in _datacontext.tbAlunos
                    select m).ToList();
        }

        public void InserirAluno(tbAluno aluno)
        {
            _datacontext.tbAlunos.InsertOnSubmit(aluno);
            _datacontext.SubmitChanges();
        }

        public void AlterarAluno(tbAluno a)
        {
            tbAluno aluno = (from m in _datacontext.tbAlunos
                             where m.Ra == a.Ra
                             select m).SingleOrDefault();
            
            aluno.Nome = a.Nome;

            _datacontext.SubmitChanges();

        }

        public void ExcluirAluno(tbAluno a)
        {
            tbAluno aluno = (from m in _datacontext.tbAlunos
                             where m.Ra == a.Ra
                             select m).SingleOrDefault();

            _datacontext.tbAlunos.DeleteOnSubmit(aluno);

            _datacontext.SubmitChanges();

        }

        #endregion

        #region Disciplina

        public tbDisciplina BuscarDisciplina(int Codigo)
        {
            return (from m in _datacontext.tbDisciplinas
                    where m.Codigo == m.Codigo
                    select m).SingleOrDefault();
        }

        public List<tbDisciplina> ListarDisciplina()
        {
            return (from m in _datacontext.tbDisciplinas
                    select m).ToList();
        }

        public void InserirDisciplina(tbDisciplina disc)
        {
            _datacontext.tbDisciplinas.InsertOnSubmit(disc);
            _datacontext.SubmitChanges();
        }

        public void AlterarDisciplina(tbDisciplina d)
        {
            tbDisciplina disc = (from m in _datacontext.tbDisciplinas
                             where m.Codigo == d.Codigo
                             select m).SingleOrDefault();

            disc.Disciplina = d.Disciplina;

            _datacontext.SubmitChanges();

        }

        public void ExcluirDisciplina(tbDisciplina d)
        {
            tbDisciplina disc = (from m in _datacontext.tbDisciplinas
                             where m.Codigo == d.Codigo
                             select m).SingleOrDefault();

            _datacontext.tbDisciplinas.DeleteOnSubmit(disc);

            _datacontext.SubmitChanges();

        }

        #endregion

        #region Aluno Disciplina

        public List<tbAlunoDisc> ListarAlunoDisciplina()
        {
            return (from m in _datacontext.tbAlunoDiscs
                    select m).ToList();
        }

        public void MatricularAluno(tbAlunoDisc ad)
        {
            _datacontext.tbAlunoDiscs.InsertOnSubmit(ad);
            _datacontext.SubmitChanges();
        }


        public void ExcluirMatricula(tbAlunoDisc ad)
        {
            tbAlunoDisc aluno = (from m in _datacontext.tbAlunoDiscs
                             where m.Ra == ad.Ra && m.Codigo == ad.Codigo
                             select m).SingleOrDefault();

            _datacontext.tbAlunoDiscs.DeleteOnSubmit(aluno);

            _datacontext.SubmitChanges();

        }

        #endregion


    }
}